//
//  HomeMessageViewcontroller.m
//  CarUser
//
//  Created by 王森林 on 2018/12/6.
//  Copyright © 2018年 王森林. All rights reserved.
//

#import "HomeMessageViewcontroller.h"
#import "HomeNoticeCell.h"
#import "HomeNoticeDetailViewController.h"
@interface HomeMessageViewcontroller()
@property (nonatomic, strong) NSMutableArray *listAry;
@end


@implementation HomeMessageViewcontroller

-  (void)viewDidLoad
{
    [super viewDidLoad];
    [self createUI];
    [self fetchDataFromNet];
    self.mainTableView.mj_header = [Custom_Mjfresh mj_customMjfreshHeaderSetting:^{
        [self fetchDataFromNet];
    }];
}


- (void)fetchDataFromNet
{
    //获取广告顶部
    NSDictionary *params = @{
                             @"FKEY":[CommonMethod createMD5Key:@"NULL"],
                             @"zxfl00":self.type
                             
                             };
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showWithStatus:nil];
    });
    
    [[ZY_AfnNetManager shareInstance]postWithURL:Car_API_1 params:params md5Paraname:nil success:^(id  _Nullable response) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        [self.mainTableView.mj_header endRefreshing];
        [self.listAry removeAllObjects];
        NSDictionary *resultDict  = response;
        NSArray *ary = [resultDict objectForKey:@"list"];
        if (ary != nil && ![ary isKindOfClass:[NSNull class]] && ary.count != 0) {
            self.listAry = [NSMutableArray arrayWithArray:[resultDict objectForKey:@"list"]];
            [self hideNoDataView];
        } else {
             [self showNodataView:self.mainTableView.centerY-150 withView:self.mainTableView];
            
        }
        [self.mainTableView  reloadData];
//        self.imgsAry  = [resultDict objectForKey:@"list"];
//        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
//        [self.mainTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
//        
        
    } fail:^(NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
             [self.mainTableView.mj_header endRefreshing];
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        });
        
    }];
    
    
}

- (void)createUI
{
    self.mainTableView.frame  = CGRectMake(0, 0, BOUNDWIDTH, BOUNDHEIGHT-(IS_IPHONE_X?88:64)-45);
    [self.mainTableView registerClass:[HomeNoticeCell class] forCellReuseIdentifier:@"HomeNoticeCell"];
    
}

#pragma mark - 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listAry.count;
}
#pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 260.0f;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeNoticeCell *cell = (HomeNoticeCell *)[tableView dequeueReusableCellWithIdentifier:@"HomeNoticeCell" forIndexPath:indexPath];
    [cell setData:self.listAry[indexPath.row]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dict  = self.listAry[indexPath.row];
    HomeNoticeDetailViewController *controller = [[HomeNoticeDetailViewController alloc] init];
    controller.ID = [dict objectForKey:@"ARTICLE_ID"];
    if (self.type.integerValue == 4) {
        controller.title = @"通知详情";
    }
    if (self.type.integerValue == 5) {
        controller.title = @"广播详情";
    }
    [self.navigationController pushViewController:controller animated:YES];
    
}

@end
